<template>
    <div>
        <h1>食谱新增</h1>
        <table class="table">
            <tbody>
                <tr>
                    <td>食谱标题</td>
                    <td><input type="text" v-model="info.Name"></td>
                </tr>
                <tr>
                    <td>分类</td>
                    <td><select v-model="info.RetId">
                            <option value="">请选择</option>
                            <option v-for="a in lisr" :value="a.Id">{{ a.RetName }}</option>
                        </select></td>
                </tr>
                <tr>
                    <td>图片</td>
                    <td><input type="file" @change="Imgs">
                        <img :src="info.Img" width="100" height="100" v-if="info.Img != ''">
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="button" value="添加" @click="ok"></td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router';
import axios from 'axios';
const router = useRouter();
let info: any = reactive({
    "Id": 0,
    "Name": "",
    "RetId": "",
    "Img": ""
})
let lisr: any = ref([]);
onMounted(() => {
    getlisr();
})
const getlisr = () => {
    axios({
        url: '/api/User/GetFL',
        method: 'get'
    })
        .then(res => {
            console.log(res);
            lisr.value = res.data
        })
        .catch(err => {
            console.log(err);

        })
}
//添加
const ok = () => {
    if (info.Name == "") {
        alert("食谱标题不能为空")
        return;
    }
    if (info.RetId == "") {
        alert("分类不能为空")
        return;
    }
    if (info.Img == "") {
        alert("图片不能为空")
        return;
    }

    axios({
        url: '/api/User/RetAdd',
        method: 'post',
        data: info
    })
        .then(res => {
            if (res.data > 0) {
                alert("添加成功");
                router.push('/');
                return;
            }
            else {
                alert("添加失败");
                return;
            }
        })
        .catch(err => {
            console.log(err);

        })
}
const Imgs = (e: any) => {
    let obj = e.target.files[0];
    let f = new FormData();
    f.append('file', obj)
    axios({
        url: '/api/User/Imgs',
        method: 'post',
        data: f
    })
        .then(res => {
            console.log(res);
            info.Img = res.data
        })
        .catch(err => {
            console.log(err);
        })
}
</script>

<style scoped></style>